package com.mfs.manager.controller;

import com.mfs.common.enums.ResponseCodeEnum;
import com.mfs.common.response.ResponseUtil;
import com.mfs.common.utills.StringUtil;
import com.mfs.manager.log.Log;
import com.mfs.manager.service.dto.*;
import com.mfs.proxy.core.constant.AppConstant;
import com.mfs.manager.service.ProxyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProxyController {

    @Autowired
    private ProxyService proxyService;

    /**
     * 为代理客户端分配代理服务器
     * @param request 请求入参
     * @return ProxyMallocResponse
     */
    @Log
    @PostMapping("/malloc")
    public ProxyMallocResponse malloc(@RequestBody ProxyMallocRequest request) {
        if (request == null || StringUtil.isBlank(request.getTargetIp())
                || StringUtil.isBlank(request.getUserCode())
                || request.getTargetPort() == null) {

            return ResponseUtil.createResponse(ResponseCodeEnum.PARAMS_ERROR, ProxyMallocResponse.class);
        }

        // 超过代理范围的端口不代理
        if (request.getTargetPort() < AppConstant.MIN_PROXY_PORT || request.getTargetPort() > AppConstant.MAX_PROXY_PORT) {
            return ResponseUtil.createResponse(ResponseCodeEnum.PARAMS_ERROR,"仅支持代理[1025,65535]范围内的端口", ProxyMallocResponse.class);
        }
        return proxyService.malloc(request);
    }

    /**
     * 代理服务端回调接口，验证代理客户端握手请求是否已经经过分配
     * 验证通过则新增一条客户端通道
     * @param
     * */
    @Log
    @PostMapping("/malloc/verify")
    public VerifyMallocResponse verifyMalloc(@RequestBody VerifyMallocRequest request) {
        if (request == null || StringUtil.isBlank(request.getUserCode())
                || StringUtil.isBlank(request.getTargetIp()) || request.getTargetPort() == null
                || StringUtil.isBlank(request.getOutIp()) || request.getProxyPort() == null
                || request.getTargetPort() < AppConstant.MIN_PROXY_PORT || request.getTargetPort() > AppConstant.MAX_PROXY_PORT
                || request.getProxyPort() < AppConstant.MIN_PROXY_PORT || request.getProxyPort() > AppConstant.MAX_PROXY_PORT) {
            return ResponseUtil.createResponse(ResponseCodeEnum.PARAMS_ERROR, VerifyMallocResponse.class);
        }
        return proxyService.verifyMalloc(request);
    }

    /**
     * 代理服务端回调接口，减少一条客户端通道
     * 如果当前通道是最后一条则同时关闭该代理分配
     */
    @Log
    @PostMapping("/close")
    public CloseClientChannelResponse closeClientChannel(@RequestBody CloseClientChannelRequest request) {
        if (request == null || StringUtil.isBlank(request.getOutIp())
                || StringUtil.isBlank(request.getProxyIp()) || request.getProxyPort() == null) {
            return ResponseUtil.createResponse(ResponseCodeEnum.PARAMS_ERROR, CloseClientChannelResponse.class);
        }
        return proxyService.closeClientChannel(request);
    }

    /**
     * 代理服务端接口，注册代理服务器
     *
     * */
    @Log
    @PostMapping("/server/register")
    public ServerRegisterResponse serverRegister(@RequestBody ServerRegisterRequest request) {
        if (request == null || StringUtil.isBlank(request.getIp()) || StringUtil.isBlank(request.getOutIp())) {
            return ResponseUtil.createResponse(ResponseCodeEnum.PARAMS_ERROR, ServerRegisterResponse.class);
        }
        return proxyService.serverRegister(request);
    }

    /**
     * 代理服务端接口，注销代理服务器
     * */
    @Log
    @PostMapping("/server/unregister")
    public ServerUnregisterResponse serverUnregister(@RequestBody ServerUnregisterRequest request) {
        if (request == null || StringUtil.isBlank(request.getIp()) || StringUtil.isBlank(request.getOutIp())) {
            return ResponseUtil.createResponse(ResponseCodeEnum.PARAMS_ERROR, ServerUnregisterResponse.class);
        }
        return proxyService.serverUnregister(request);
    }
}
